package cn.itcast.bos.web.action;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: SOS
 * @create: 2018-03-02 15:45
 */

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class OrderAction extends BaseAction<Order> {

    private String sendAreaInfo;//寄件人省市区信息
    private String recAreaInfo; //收件人省市区信息

    public void setSendAreaInfo(String sendAreaInfo) {
        this.sendAreaInfo = sendAreaInfo;
    }

    public void setRecAreaInfo(String recAreaInfo) {
        this.recAreaInfo = recAreaInfo;
    }

    @Action(value = "order_add", results = {@Result(name = SUCCESS, location = "./index.html", type = "redirect"),
            @Result(name = "input", location = "./index.html", type = "redirect")})
    public String orderSave() {

        //封装数据
        String[] sendAreaData = sendAreaInfo.split("/");
        String[] recAreaData = recAreaInfo.split("/");
        Area sendArea = new Area();
        Area recArea = new Area();
        sendArea.setProvince(sendAreaData[0]);
        sendArea.setCity(sendAreaData[1]);
        sendArea.setDistrict(sendAreaData[2]);

        recArea.setProvince(recAreaData[0]);
        recArea.setCity(recAreaData[1]);
        recArea.setDistrict(recAreaData[2]);

        model.setSendArea(sendArea);
        model.setRecArea(recArea);

        Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
        model.setCustomer_id(customer.getId());
        WebClient.create(Constants.BOS_MANAGEMENT_URL + "/services/orderService/order").type(MediaType
                .APPLICATION_JSON).post(model);
        System.out.println("保存订单成功!!!!!!!!!!!!");
        return SUCCESS;
    }
}
